/**
 * 
 */
package org.lora.webframework.monitor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;

import org.lora.core.annotation.Action;
import org.lora.core.context.WebContext;
import org.lora.mvc.annotation.RequestPath;
import org.lora.mvc.annotation.ResponseJSON;
import org.lora.mvc.entity.ViewData;
import org.lora.util.StringUtil;
import org.lora.webframework.common.CommonConstant;
import org.lora.webframework.common.JsonResult;
import org.lora.webframework.common.WebframeBaseAction;

/**
 * <p>
 * 类名:ConnectionPoolAction
 * </p>
 * 描述:数据库连接池监控Action<br>
 * 创建时间:2016年11月19日 上午10:56:22<br>
 * 
 * @author Bladnir@outlook.com<br>
 *         修改历史:<br>
 *         2016年11月19日 Bladnir 创建<br>
 */
@Action
@RequestPath(path = "/webframework/monitor/connectionPool")
public class ConnectionPoolAction extends WebframeBaseAction {

	/**
	 * 打开系统信息页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestPath(path = "/showConnectionPool")
	public ViewData showBaseInfo() {
		return new ViewData("/jsp/webframework/monitor/connectionPool.jsp");
	}

	/**
	 * 获取数据库连接池下拉框数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/getPoolDataNames")
	@ResponseJSON
	public JsonResult getPoolDataNames() {
		JsonResult result = new JsonResult();
		result.setFlag(CommonConstant.FAIL);
		try {
			Map<String, DataSource> dataSourceMap = WebContext.getInstance().getDataSourceMap();

			List<Map<String, Object>> list = new ArrayList<>();

			for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {

				DataSource ds = entry.getValue();

				if (ds instanceof org.apache.dataconn.jdbc.pool.DataSource) {
					org.apache.dataconn.jdbc.pool.DataSource tomcatJDBCDataSource = (org.apache.dataconn.jdbc.pool.DataSource) ds;
					int maxActive = tomcatJDBCDataSource.getMaxActive();
					int maxIdle = tomcatJDBCDataSource.getMaxIdle();

					Map<String, Object> map = new HashMap<>();
					map.put("maxActive", maxActive);
					map.put("maxIdle", maxIdle);
					map.put("poolName", entry.getKey());

					list.add(map);
				}

			}

			result.setData(list);
			result.setFlag(CommonConstant.SUCCESS);
		} catch (Exception e) {
			super.businessLogger.error(e.getMessage(), e);
		}

		return result;
	}

	/**
	 * 获取数据库连接池状态数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/getPoolData")
	@ResponseJSON
	public JsonResult getPoolData(HttpServletRequest request) {
		JsonResult result = new JsonResult();
		result.setFlag(CommonConstant.FAIL);
		String poolName = request.getParameter("poolName");
		try {

			long currentTimeLong = System.currentTimeMillis();

			DataSource ds = null;

			Map<String, DataSource> dataSourceMap = WebContext.getInstance().getDataSourceMap();

			for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {

				if (StringUtil.isNull(poolName) || poolName.equals(entry.getKey())) {
					ds = entry.getValue();
					break;
				}
			}

			Map<String, Object> poolStatMap = new HashMap<>();

			if (ds instanceof org.apache.dataconn.jdbc.pool.DataSource) {
				org.apache.dataconn.jdbc.pool.DataSource tomcatJDBCDataSource = (org.apache.dataconn.jdbc.pool.DataSource) ds;

				org.apache.dataconn.jdbc.pool.ConnectionPool cp = tomcatJDBCDataSource.getPool();
				if (cp != null) {
					poolStatMap.put("idleNumber", cp.getIdle());
					poolStatMap.put("activeNumber", cp.getActive());
				} else {
					poolStatMap.put("idleNumber", 0);
					poolStatMap.put("activeNumber", 0);
				}

				poolStatMap.put("nowTime", currentTimeLong);

			}
			result.setData(poolStatMap);
			result.setFlag(CommonConstant.SUCCESS);
		} catch (Exception e) {
			super.businessLogger.error(e.getMessage(), e);
		}

		return result;
	}

}
